package com.foredu.fiqs.web.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foredu.fiqs.domain.Salary;
import com.foredu.fiqs.domain.User;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.service.ISalaryQueryService;
import com.foredu.fiqs.web.actionform.SalaryQueryActionForm;

/**
 * Description:<br/>
 * 处理工资明细查询请求，显示当月或历史工资明细<br/>
 * Date:<br/>
 * 2014/5/16<br/>
 * 
 * @author hushigang
 */

public class SalaryQueryAction extends AbstractBaseDispatchAction {

	private ISalaryQueryService salaryQueryService;
	private IManagementService managementService;

	//查询当前月份工资明细
	public ActionForward queryCurrentSalary(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得表单数据
		SalaryQueryActionForm sqform = (SalaryQueryActionForm) form;
		int salaryItemId = sqform.getSalaryItemId();
		//首先获取Session中保存的用户信息，根据用户的ID去Salary表中查询总工资
		// 获取当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
					.getUsername());
		} catch (ClassCastException e) {
			// 用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
		//获取系统当前的年份和月份
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		Salary salary = salaryQueryService.findSalaryByUserIdAndDate(user.getId(), month, year);
		//然后根据SalaryId去SalaryItem表中查询对应的工资条目
		List<Map<String, Object>> itemList = salaryQueryService.findSalItemBySalaryId(salary.getId());
		//用户选择需要具体查看的某个工资项
		if(salaryItemId != 0||!"".equals(salaryItemId)){
			List<Map<String, Object>> salaryDetailList = salaryQueryService.findSalDetailBySalItemId(salaryItemId);
			request.setAttribute("salaryDetailList", salaryDetailList);
		}
		System.out.println(itemList.size());
		request.setAttribute("itemList", itemList);
		request.setAttribute("user", user);
		request.setAttribute("salary", salary);
		return mapping.findForward("queryCurrentSalary");
	}

	//查询历史月份工资明细
	public ActionForward queryHistorySalary(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得表单数据
		SalaryQueryActionForm sqform = (SalaryQueryActionForm) form;
		int salaryItemId = sqform.getSalaryItemId();
		int month = sqform.getMonth();
		int year = sqform.getYear();
		// 获取当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
					.getUsername());
		} catch (ClassCastException e) {
			// 用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
		Salary salary = salaryQueryService.findSalaryByUserIdAndDate(user.getId(), month, year);
		//然后根据SalaryId去SalaryItem表中查询对应的工资条目
		List<Map<String, Object>> itemList = salaryQueryService.findSalItemBySalaryId(salary.getId());
		//用户选择需要具体查看的某个工资项
		if(salaryItemId != 0||!"".equals(salaryItemId)){
			List<Map<String, Object>> salaryDetailList = salaryQueryService.findSalDetailBySalItemId(salaryItemId);
			request.setAttribute("salaryDetailList", salaryDetailList);
		}
		System.out.println(itemList.size());
		request.setAttribute("itemList", itemList);
		request.setAttribute("user", user);
		request.setAttribute("salary", salary);
		request.setAttribute("month", month);
		request.setAttribute("year", year);
		return mapping.findForward("queryHistorySalary");
	}

	public void setSalaryQueryService(ISalaryQueryService salaryQueryService) {
		this.salaryQueryService = salaryQueryService;
	}


	public ISalaryQueryService getSalaryQueryService() {
		return salaryQueryService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}	
	
}
